package cn.openea.perpartner.payment.web.subpoints.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.payment.web.subpoints.entity.SubPoint;
import cn.openea.perpartner.payment.web.subpoints.dto.SubPointDto;
import cn.openea.perpartner.payment.web.subpoints.entity.SubPointLog;
import cn.openea.perpartner.payment.web.subpoints.service.ISubPaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 积分子系统控制层
 * @author makejava
 * @since 2022-06-27 14:45:38
 */
@Api(tags = "积分子系统")
@RestController
@RequestMapping("/subpay")
public class SubPointController extends BaseController {
    @Resource
    private ISubPaymentService subPaymentService;
    @ApiOperation("收入明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "sceneId", value = "场景id"),
            @ApiImplicitParam(name = "pageSize", value = "当前页"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数据量"),
    })
    @PostMapping("/revenue/list")
    public TableDataInfo selectRevenueList(@RequestBody @Valid SubPointDto dto) {
        startPage();
        List<SubPointLog> list = subPaymentService.selectList(dto.getUserId(), dto.getSceneId(),"add");
        return getDataTable(list);
    }
    @ApiOperation("支出明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "pageSize", value = "当前页"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数据量"),
    })
    @PostMapping("/outlay/list")
    public TableDataInfo selectOutlayList(@RequestBody @Valid SubPointDto dto) {
        startPage();
        List<SubPointLog> list = subPaymentService.selectList(dto.getUserId(),dto.getSceneId(),"sub");
        return getDataTable(list);
    }

    @ApiOperation("可用积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "sceneId", value = "场景id")
    })
    @PostMapping("/getPoints")
    public AjaxResult selectPointsByUserId(@RequestBody @Valid SubPointDto dto) {
        SubPoint paymentPoints = this.subPaymentService.selectPointsByUserId(dto.getUserId(),dto.getSceneId());
        if (BeanUtil.isEmpty(paymentPoints)){
            paymentPoints = new SubPoint();
            paymentPoints.setPoints("0");
        }
        return success(Double.parseDouble(paymentPoints.getPoints()));
    }
    @ApiOperation("累计积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "sceneId", value = "场景id")
    })
    @PostMapping("/getTotalPoints")
    public AjaxResult  getTotalPoints(@RequestBody @Valid SubPointDto dto)  {
        double totalPoints = subPaymentService.getTotalPoints(dto.getUserId(),dto.getSceneId());
        return success(totalPoints);
    }

    @ApiOperation("增加积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "增加积分"),
            @ApiImplicitParam(name = "reason", value = "增加积分原因"),
            @ApiImplicitParam(name = "sceneId", value = "场景id")
    })
    @PostMapping("/add")
    public AjaxResult add( @RequestBody @Valid SubPointDto dto) {
        return this.subPaymentService.add(dto)?AjaxResult.success():AjaxResult.error();
    }
    @ApiOperation("减少积分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "减少积分"),
            @ApiImplicitParam(name = "reason", value = "减少积分原因"),
            @ApiImplicitParam(name = "sceneId", value = "场景id")
    })
    @PostMapping("/sub")
    public AjaxResult sub(@RequestBody @Valid SubPointDto dto) {
        return this.subPaymentService.sub(dto)?success():error();
    }
    @ApiOperation("积分兑入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "兑入积分"),
            @ApiImplicitParam(name = "reason", value = "兑入原因"),
            @ApiImplicitParam(name = "sceneId", value = "场景id"),
    })
    @PostMapping("/extract")
    public AjaxResult extract(@RequestBody @Valid SubPointDto dto) {
        return this.subPaymentService.extract(dto)?success():error("平台积分不足！");
    }
    @ApiOperation("积分兑出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "兑出积分"),
            @ApiImplicitParam(name = "reason", value = "兑出原因"),
            @ApiImplicitParam(name = "sceneId", value = "场景id"),
    })
    @PostMapping("/redeem")
    public AjaxResult redeem(@RequestBody @Valid SubPointDto dto) {
        return this.subPaymentService.redeem(dto)?success():error("积分不足！");
    }

    @ApiOperation("转入钱包")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "points", value = "所需积分"),
            @ApiImplicitParam(name = "sceneId", value = "场景id"),
            @ApiImplicitParam(name = "reason", value = "转入原因"),
    })
    @PostMapping("/toWallet")
    public AjaxResult toWallet(@RequestBody SubPointDto dto) {
        boolean flag = subPaymentService.pointsToWallet(dto);
        return flag ? success() : error("操作失败，请检查可用积分是否充足！");
    }

}

